<?php
class TeacherAction extends Action {

    public function _initialize() {
        $Config = A('Config');
        $Config->checkLogIn();
        if (!in_array(ACTION_NAME, array('edit', 'update'))) {
            $Config->checkUserLevel(array('1'));
        } else {
            $Config->checkUserLevel(array('1', '3'));
        }
    }
    
    public function index() {
        $User = M('User');
        import("ORG.Util.Page");
        $count = $User->where(array('user_type_id' => 3))->count();
        $Page = new Page($count, 25);
        $show = $Page->show();
        $this->assign('page',$show);
        
        $user = $User->where(array('econ_user.user_type_id' => 3))->order(array('user_id' => 'asc'))->join(array('econ_user_type ON econ_user.user_type_id = econ_user_type.user_type_id'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('user', $user);
        
        // $Category = M('Category');
        // $category = $Category->where(array('cat_type_id' => 3))->order(array('cat_id' => 'asc'))->select();
        // dump($category);
        // $this->assign('category', $category);
        
        $this->display();
    }
    
    public function add() {
        $Category = M('Category');
        $category = $Category->where(array('cat_type_id' => 3))->order(array('cat_id' => 'asc'))->select();
        
        $this->assign('category', $category);
        $this->display();
    }
    
    public function edit() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            
            if ($id != $_SESSION['user']['user_id']) {
                exit();
            }
            
            $User = M('User');
            $user = $User->find($id);
            
            // $UserType = M('UserType');
            // $user_type = $UserType->order(array('user_type_id' => 'asc'))->select();
            
            $this->assign('user', $user);
            // $this->assign('user_type', $user_type);
            
            $Category = M('Category');
            $category = $Category->where(array('cat_type_id' => 3))->order(array('cat_id' => 'asc'))->select();
            $this->assign('category', $category);
            
            /* 获取教师信息 */
            $Teacher = M('Teacher');
            $teacher = $Teacher->find($user['teacher_id']);
            $this->assign('teacher', $teacher);
            
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
    /* 更新教师信息 */
    public function update() {
        if (isset($_POST['submitted'])) {
        
            if ($_POST['user_id'] != $_SESSION['user']['user_id']) {
                exit();
            }
        
            $user_data = array();
            $user_data['user_id'] = $_POST['user_id'];
            $user_data['user_name'] = $_POST['user_name'];
            if (trim($_POST['password']) != '') {
                $user_data['password'] = $_POST['password'];
            }
            $user_data['re_password'] = $_POST['re_password'];
            $user_data['cat_id'] = $_POST['cat_id'];
            
            $teacher_data = array();
            $teacher_data['teacher_id'] = $_POST['teacher_id'];
            $teacher_data['introduction'] = $_POST['introduction'];
            $teacher_data['success'] = $_POST['success'];
            $teacher_data['course'] = $_POST['course'];
            $teacher_data['project'] = $_POST['project'];
            $teacher_data['contact'] = $_POST['contact'];
            
            $User = D('User');
            $Teacher = M('Teacher');
            
            if ($User->create()) {
                unset($user_data['re_password']);
                if ((false !== $User->save($user_data)) && (false !== $Teacher->save($teacher_data))) {
                    $this->success('成功编辑信息！');
                } else {
                    $this->error('编辑信息失败！');
                }
            } else {
                $this->error($User->getError());
            }
            
            // $password = $_POST['password'];
            // if ($password == '') {
                // $validate = array(
                    // array('user_name', 'checkUserName', '用户名必须为3-14个字节！', 0, 'callback'),
                    // array('user_type_id', array(1, 2, 3), '请选择用户类型！', 2, 'in')
                // );
                // $User->setProperty("_validate", $validate);
                
                // $auto = array ();
                // $User->setProperty("_auto", $auto);
            // }
            
            // if ($user = $User->create()) {
                // unset($user['password']);
                // if (false !== $User->save($user)) {
                    // $this->success('成功编辑用户！');
                // } else {
                    // $this->error('编辑用户失败！');
                // }
            // } else {
                // $this->error($User->getError());
            // }
        }
    }
    
    /* 插入教师信息 */
    public function insert() {
        if (isset($_POST['submitted'])) {
            // $user_data = array();
            // $user_data['user_name'] = $_POST['user_name'];
            // $user_data['user_type_id'] = $_POST['user_type_id'];
            // $user_data['password'] = $_POST['password'];
            // $user_data['re_password'] = $_POST['re_password'];
            // $user_data['cat_id'] = $_POST['cat_id'];
            
            $teacher_data = array();
            $teacher_data['introduction'] = $_POST['introduction'];
            $teacher_data['success'] = $_POST['success'];
            $teacher_data['course'] = $_POST['course'];
            $teacher_data['project'] = $_POST['project'];
            $teacher_data['contact'] = $_POST['contact'];
            
            $User = D('User');
            $Teacher = M('Teacher');
            
            if ($User->create()) {
            // if ($User->create($user_data)) {
                if (($user_id = $User->add()) && ($teacher_id = $Teacher->add($teacher_data))) {
                // if ($user_id = $User->add()) {
                    $User->find($user_id);
                    $User->teacher_id = $teacher_id;
                    $User->save();
                    $this->success('成功添加教师！');
                } else {
                    $this->error('添加教师失败！');
                }
            } else {
                $this->error($User->getError());
            }
            

            // if ($art = $Article->create()) {
                // if ($Article->add()) {
                    // $this->success('成功添加文章！');
                // } else {
                    // $this->error('添加文章失败！');
                // }
            // } else {
                // $this->error($Article->getError());
            // }
        }
    }

    public function catIndex() {
        $Category = M('Category');
        import("ORG.Util.Page");
        $count = $Category->where(array('cat_type_id' => 3))->count();
        $Page = new Page($count, 25);
        $show = $Page->show();
        $this->assign('page',$show);
        
        $category = $Category->where(array('cat_type_id' => 3))->order(array('cat_id' => 'asc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('category', $category);
        $this->display();
    }
    
    public function addCat() {
        $this->display();
    }
    
    public function editCat() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Category = M('Category');
            $category = $Category->find($id);
            
            $this->assign('category', $category);
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
}
?>